﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Client.Models
{
    public class InvoiceQueryModel
    {
        public string NSRSBH { get; set; }
        public string FJH { get; set; }
        public string FPZL { get; set; }
        public string FPDM { get; set; }
        public string FPHM { get; set; }
        public string SYH { get; set; }

        public override string ToString()
        {
            try
            {
                return string.Format("<?xml version=\"1.0\" encoding=\"GBK\"?>{0}<INPUT>{0}<NSRSBH>{1}</NSRSBH>{0}<FJH>{2}</FJH>{0}<FPZL>{3}</FPZL>{0}<FPDM>{4}</FPDM>{0}<FPHM>{5}</FPHM>{0}<SYH>{6}</SYH>{0}</INPUT>", Environment.NewLine, NSRSBH, FJH, FPZL, FPDM, FPHM, SYH);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    /// <summary>
    /// 发票查询返回模型
    /// </summary>
    public class InvoiceQueryReturn
    {
        //---------RETURN字段----------
        public string CODE { get; set; }
        public string MESS { get; set; }
        //---------DATA字段----------
        /// <summary>
        /// 发票流水号
        /// </summary>
        public string FPLSH { get; set; }
        /// <summary>
        /// 发票代码
        /// </summary>
        public string FPDM { get; set; }
        /// <summary>
        /// 发票号码 
        /// </summary>
        public string FPHM { get; set; }
        /// <summary>
        /// 发票各类
        /// </summary>
        public string FPZL { get; set; }
        /// <summary>
        /// 索引号
        /// </summary>
        public string SYH { get; set; }
        /// <summary>
        /// 顺序号
        /// </summary>
        public string SXH { get; set; }
        /// <summary>
        /// 开票类型0蓝字；1红字；2空白作废；3蓝票作废；4红票作废
        /// </summary>
        public string KPLX { get; set; }
        /// <summary>
        /// 上传标志0已上传，1未上传
        /// </summary>
        public int SCBZ { get; set; }
        /// <summary>
        /// 开票时间
        /// </summary>
        public DateTime KPSJ { get; set; }
        /// <summary>
        /// 发票防伪码
        /// </summary>
        public string FWM { get; set; }
        /// <summary>
        /// 二维码
        /// </summary>
        public string EWM { get; set; }
        /// <summary>
        /// 51二维码
        /// </summary>
        public string EWM51 { get; set; }
        /// <summary>
        /// PDF路径
        /// </summary>
        public string PDFLJ { get; set; }
        /// <summary>
        /// 发票密文（BASE64字符串）
        /// </summary>
        public string FPMW { get; set; }
        /// <summary>
        /// 销方税号
        /// </summary>
        public string XFSH { get; set; }
        /// <summary>
        /// 销方名称
        /// </summary>
        public string XFMC { get; set; }
        /// <summary>
        /// 销方地址电话
        /// </summary>
        public string XFDZDH { get; set; }
        /// <summary>
        /// 销方银行账号
        /// </summary>
        public string XFYHZH { get; set; }
        /// <summary>
        /// 购方税号
        /// </summary>
        public string GFSH { get; set; }
        /// <summary>
        /// 购方名称
        /// </summary>
        public string GFMC { get; set; }
        /// <summary>
        /// 购方地址电话
        /// </summary>
        public string GFDZDH { get; set; }
        /// <summary>
        /// 购方银行账号
        /// </summary>
        public string GFYHZH { get; set; }
        /// <summary>
        /// 合计金额（不含税）
        /// </summary>
        public decimal HJJE { get; set; }
        /// <summary>
        /// 合计税额
        /// </summary>
        public decimal HJSE { get; set; }
        /// <summary>
        /// 价税合计
        /// </summary>
        public decimal JSHJ { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string BZ { get; set; }
        /// <summary>
        /// 收款人
        /// </summary>
        public string SKR { get; set; }
        /// <summary>
        /// 开票人
        /// </summary>
        public string KPR { get; set; }
        /// <summary>
        /// 复核人
        /// </summary>
        public string FHR { get; set; }
        /// <summary>
        /// 原发票代码
        /// </summary>
        public string YFPDM { get; set; }
        /// <summary>
        /// 原发票号码
        /// </summary>
        public string YFPHM { get; set; }
        /// <summary>
        /// 作废时间
        /// </summary>
        public DateTime ZFSJ { get; set; }
        /// <summary>
        /// 签名（318个字符）
        /// </summary>
        public string SIGN { get; set; }
        /// <summary>
        /// 清单标志
        /// </summary>
        public string QDBZ { get; set; }
        /// <summary>
        /// 报税期数
        /// </summary>
        public string BSQS { get; set; }
        /// <summary>
        /// 编码版本号
        /// </summary>
        public string BMBBH { get; set; }
        /// <summary>
        /// 营业税标志0000000010中外合作油气田（原海洋石油）5%、1.5%；0000000020差额税；0000000000其他
        /// </summary>
        public string YYSBZ { get; set; }
        /// <summary>
        /// 红票通知单编号
        /// </summary>
        public string HPTZDBH { get; set; }
        /// <summary>
        /// 商品明细行数量
        /// </summary>
        public int SFXMXXCOUNT { get; set; }
        /// <summary>
        /// 商品明细行数组
        /// </summary>
        public InvoiceEntryModel[] SFXMXX { get; set; }

        public void ResolveXML(string xmlData)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlData))
                    throw new Exception("数据不能为空！");
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlData);
                XmlElement xl = xDoc.DocumentElement;
                var xList = xl.GetElementsByTagName("RETURN");
                if (xList.Count < 1)
                    throw new Exception("数据格式不正确！");
                XmlElement xl1 = (XmlElement)xList[0];
                xList = xl1.GetElementsByTagName("CODE");
                CODE = xList.Count > 0 ? xList[0].InnerText : "";
                xList = xl1.GetElementsByTagName("MESS");
                MESS = xList.Count > 0 ? xList[0].InnerText : "";
                xList = xl.GetElementsByTagName("DATA");
                if (xList.Count < 1)
                    throw new Exception("数据格式不正确！");
                xl1 = (XmlElement)xList[0];
                xList = xl1.GetElementsByTagName("FPCX");
                if (xList.Count < 1)
                    throw new Exception("数据格式不正确！");
                XmlNode xn = xList[0];
                FPLSH = getXmlNodeText(xn, "FPLSH");
                FPDM = getXmlNodeText(xn, "FPDM");
                FPHM = getXmlNodeText(xn, "FPHM");
                FPZL = getXmlNodeText(xn, "FPZL");
                SYH = getXmlNodeText(xn, "SYH");
                SXH = getXmlNodeText(xn, "SXH");
                KPLX = getXmlNodeText(xn, "KPLX");
                SCBZ = Convert.ToInt32(getXmlNodeText(xn, "SCBZ"));
                string tmp = getXmlNodeText(xn, "KPSJ");
                KPSJ = string.IsNullOrEmpty(tmp) ? default(DateTime) : DateTime.ParseExact(tmp, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                FWM = getXmlNodeText(xn, "FWM");
                EWM = getXmlNodeText(xn, "EWM");
                EWM51 = getXmlNodeText(xn, "EWM51");
                PDFLJ = getXmlNodeText(xn, "PDFLJ");
                FPMW = getXmlNodeText(xn, "FPMW");
                XFSH = getXmlNodeText(xn, "XFSH");
                XFMC = getXmlNodeText(xn, "XFMC");
                XFDZDH = getXmlNodeText(xn, "XFDZDH");
                XFYHZH = getXmlNodeText(xn, "XFYHZH");
                GFSH = getXmlNodeText(xn, "GFSH");
                GFMC = getXmlNodeText(xn, "GFMC");
                GFDZDH = getXmlNodeText(xn, "GFDZDH");
                GFYHZH = getXmlNodeText(xn, "GFYHZH");
                tmp = getXmlNodeText(xn, "HJJE");
                HJJE = string.IsNullOrEmpty(tmp) ? 0 : Convert.ToDecimal(tmp);
                tmp = getXmlNodeText(xn, "HJSE");
                HJSE = String.IsNullOrEmpty(tmp) ? 0 : Convert.ToDecimal(tmp);
                tmp = getXmlNodeText(xn, "JSHJ");
                JSHJ = String.IsNullOrEmpty(tmp) ? 0 : Convert.ToDecimal(tmp);
                BZ = getXmlNodeText(xn, "BZ");
                SKR = getXmlNodeText(xn, "SKR");
                KPR = getXmlNodeText(xn, "KPR");
                FHR = getXmlNodeText(xn, "FHR");
                YFPDM = getXmlNodeText(xn, "YFPDM");
                YFPHM = getXmlNodeText(xn, "YFPHM");
                tmp = getXmlNodeText(xn, "ZFSJ");
                ZFSJ = String.IsNullOrEmpty(tmp) ? default(DateTime) : DateTime.ParseExact(tmp, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
                SIGN = getXmlNodeText(xn, "SIGN");
                QDBZ = getXmlNodeText(xn, "QDBZ");
                BSQS = getXmlNodeText(xn, "BSQS");
                BMBBH = getXmlNodeText(xn, "BMBBH");
                YYSBZ = getXmlNodeText(xn, "YYSBZ");
                HPTZDBH = getXmlNodeText(xn, "HPTZDBH");
                SFXMXXCOUNT = Convert.ToInt32(xn.SelectSingleNode("SFXMXX").Attributes["COUNT"].Value);
                SFXMXX = null;
                if (SFXMXXCOUNT < 1)
                    return;
                SFXMXX = InvoiceEntryModel.ResolveXML(xmlData);
            }
            catch (Exception)
            {

                throw;
            }
        }
        private string getXmlNodeText(XmlNode xn, string name)
        {
            return xn.SelectSingleNode(name).InnerText;
        }

    }
    /// <summary>
    /// 发票分录模型
    /// </summary>
    public class InvoiceEntryModel
    {
        private decimal _xmdj;
        private decimal _xmsl;
        private decimal _hsdj;
        /// <summary>
        /// 发票行性质0正常行；3被折扣行；4折扣行
        /// </summary>
        public string FPHXZ { get; set; }
        /// <summary>
        /// 明细序号
        /// </summary>
        public string MXXH { get; set; }
        /// <summary>
        /// 项目名称
        /// </summary>
        public string XMMC { get; set; }
        /// <summary>
        /// 规格型号
        /// </summary>
        public string GGXH { get; set; }
        /// <summary>
        /// 计量单位
        /// </summary>
        public string JLDW { get; set; }
        /// <summary>
        /// 项目单价（不含税）
        /// </summary>
        public decimal XMDJ
        {
            get
            {
                return _xmdj;
            }
            set
            {
                _xmdj = Math.Round(value, 8, MidpointRounding.AwayFromZero);
            }
        }
        /// <summary>
        /// 项目数量8位小数
        /// </summary>
        public decimal XMSL
        {
            get
            {
                return _xmsl;
            }
            set
            {
                _xmsl = Math.Round(value, 8, MidpointRounding.AwayFromZero);
            }
        }
        /// <summary>
        /// 项目金额（不含税）
        /// </summary>
        public decimal XMJE { get; set; }
        /// <summary>
        /// 税率，2位小数；0.17
        /// </summary>
        public float SL { get; set; }
        /// <summary>
        /// 税额
        /// </summary>
        public decimal SE { get; set; }
        /// <summary>
        /// 含税单价
        /// </summary>
        public decimal HSDJ
        {
            get
            {
                return _hsdj;
            }
            set
            {
                _hsdj = Math.Round(value, 8, MidpointRounding.AwayFromZero);
            }
        }
        /// <summary>
        /// 含税金额
        /// </summary>
        public decimal HSJE { get; set; }
        /// <summary>
        /// 商品编码
        /// </summary>
        public string SPBM { get; set; }
        /// <summary>
        /// 企业自编码
        /// </summary>
        public string QYZXBM { get; set; }
        /// <summary>
        /// 是否享受优惠政策0不使用；1使用
        /// </summary>
        public string SFYHZC { get; set; }
        /// <summary>
        /// 优惠政策内容
        /// </summary>
        public string YHZCNR { get; set; }
        /// <summary>
        /// 零税率标识 空：非零 0：出口退税 1：免税 2不征收 3普通零税率
        /// </summary>
        public string LSLBS { get; set; }

        public static InvoiceEntryModel[] ResolveXML(string xmlData)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlData))
                    return null;
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlData);
                XmlElement xl = xDoc.DocumentElement;
                XmlNodeList xList = xl.GetElementsByTagName("DATA");
                if (xList.Count < 1)
                    return null;
                XmlElement xl1 = (XmlElement)xList[0];
                xList = xl1.GetElementsByTagName("FPCX");
                if (xList.Count < 1)
                    return null;
                xl1 = (XmlElement)xList[0];
                xList = xl1.GetElementsByTagName("SFXMXX");
                if (xList.Count < 1)
                    return null;
                int xCount = Convert.ToInt32(xList[0].Attributes["COUNT"].Value ?? "0");
                if (xCount <= 0)
                    return null;
                xl = (XmlElement)xList[0];
                xList = xl.GetElementsByTagName("SFXM");
                if (xList.Count != xCount)
                    throw new Exception("读取明细行出错，实际数量与定义不匹配！");
                InvoiceEntryModel[] iem = new InvoiceEntryModel[xCount];
                for (int i = 0; i < xCount; i++)
                {
                    XmlNode xn = xList[i];
                    iem[i] = new InvoiceEntryModel
                    {
                        FPHXZ = getXmlNodeText(xn, "FPHXZ"),
                        GGXH = getXmlNodeText(xn, "GGXH"),
                        HSDJ = string.IsNullOrEmpty(getXmlNodeText(xn, "HSDJ")) ? 0 : Convert.ToDecimal(getXmlNodeText(xn, "HSDJ")),
                        HSJE = string.IsNullOrEmpty(getXmlNodeText(xn, "HSJE")) ? 0 : Convert.ToDecimal(getXmlNodeText(xn, "HSJE")),
                        JLDW = getXmlNodeText(xn, "JLDW"),
                        LSLBS = getXmlNodeText(xn, "LSLBS"),
                        MXXH = getXmlNodeText(xn, "MXXH"),
                        QYZXBM = getXmlNodeText(xn, "QYZXBM"),
                        SE = string.IsNullOrEmpty(getXmlNodeText(xn, "SE")) ? 0 : Convert.ToDecimal(getXmlNodeText(xn, "SE")),
                        SFYHZC = getXmlNodeText(xn, "SFYHZC"),
                        SL = string.IsNullOrEmpty(getXmlNodeText(xn, "SL")) ? 0 : Convert.ToSingle(getXmlNodeText(xn, "SL")),
                        SPBM = getXmlNodeText(xn, "SPBM"),
                        XMDJ = string.IsNullOrEmpty(getXmlNodeText(xn, "XMDJ")) ? 0 : Convert.ToDecimal(getXmlNodeText(xn, "XMDJ")),
                        XMJE = string.IsNullOrEmpty(getXmlNodeText(xn, "XMJE")) ? 0 : Convert.ToDecimal(getXmlNodeText(xn, "XMJE")),
                        XMMC = getXmlNodeText(xn, "XMMC"),
                        XMSL = string.IsNullOrEmpty(getXmlNodeText(xn, "XMSL")) ? 0 : Convert.ToDecimal(getXmlNodeText(xn, "XMSL")),
                        YHZCNR = getXmlNodeText(xn, "YHZCNR")
                    };
                }
                return iem;
            }
            catch (Exception)
            {

                throw;
            }
        }
        private static string getXmlNodeText(XmlNode xn, string name)
        {
            return xn.SelectSingleNode(name).InnerText;
        }
    }

    
    #region 发票开具协议
    public class InvoiceIssueModel
    {
        /// <summary>
        /// 税号
        /// </summary>
        public string NSRSBH { get; set; }
        /// <summary>
        /// 分机号
        /// </summary>
        public string FJH { get; set; }
        /// <summary>
        /// 发票终端号，1-20，可空
        /// </summary>
        public string KPZDH { get; set; }
        /// <summary>
        /// 发票请求流水号
        /// </summary>
        public string FPLSH { get; set; }
        /// <summary>
        /// 小票流水号，可空
        /// </summary>
        public string XPLSH { get; set; }
        /// <summary>
        /// 发票种类，51：电子发票
        /// </summary>
        public string FPZL { get; set; }
        /// <summary>
        /// 开票类型，0蓝字 1红字
        /// </summary>
        public string KPLX { get; set; }
        /// <summary>
        /// 销方税号
        /// </summary>
        public string XFSH { get; set; }
        /// <summary>
        /// 销方名称
        /// </summary>
        public string XFMC { get; set; }
        /// <summary>
        /// 销方地址电话
        /// </summary>
        public string XFDZDH { get; set; }
        /// <summary>
        /// 销方银行账号
        /// </summary>
        public string XFYHZH { get; set; }
        /// <summary>
        /// 购方税号
        /// </summary>
        public string GFSH { get; set; }
        /// <summary>
        /// 购方名称
        /// </summary>
        public string GFMC { get; set; }
        /// <summary>
        /// 购方地址电话
        /// </summary>
        public string GFDZDH { get; set; }
        /// <summary>
        /// 购方银行账号
        /// </summary>
        public string GFYHZH { get; set; }
        /// <summary>
        /// 开票人
        /// </summary>
        public string KPR { get; set; }
        /// <summary>
        /// 收款人
        /// </summary>
        public string SKR { get; set; }
        /// <summary>
        /// 复核人
        /// </summary>
        public string FHR { get; set; }
        /// <summary>
        /// 原发票代码，开红字必须
        /// </summary>
        public string YFPDM { get; set; }
        /// <summary>
        /// 原发票号码，开红字必须
        /// </summary>
        public string YFPHM { get; set; }
        /// <summary>
        /// 红票通知单号，为空
        /// </summary>
        public string HPTZDBH { get; set; }
        /// <summary>
        /// 清单标志，为空
        /// </summary>
        public string QDBZ { get; set; }
        /// <summary>
        /// 价税合计，2位小数
        /// </summary>
        public decimal JSHJ { get; set; }
        /// <summary>
        /// 合计金额，不含税，2位小数
        /// </summary>
        public decimal HJJE { get; set; }
        /// <summary>
        /// 合计税额，2位小数
        /// </summary>
        public decimal HJSE { get; set; }
        /// <summary>
        /// 编码版本号，可不填写
        /// </summary>
        public string BMBBH { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string BZ { get; set; }
        /// <summary>
        /// 营业税标志，必填
        /// 0000000010：1.5%税率
        /// 0000000020:差额税
        /// 0000000000:其他
        /// </summary>
        public string YYSBZ { get; set; }
        /// <summary>
        /// 推送方式条数
        /// </summary>
        public int PushCount { get; set; }
        /// <summary>
        /// 推送方式明细
        /// </summary>
        public InvoicePushMethodModel[] COMMON_NODE { get; set; }
        /// <summary>
        /// 明细项目条数
        /// </summary>
        public int MXCOUNT { get; set; }
        /// <summary>
        /// 明细项目信息
        /// </summary>
        public InvoiceIssueEntryModel[] FPXMXX { get; set; }
        public override string ToString()
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                var declare = xDoc.CreateXmlDeclaration("1.0", "GBK", "yes");
                xDoc.AppendChild(declare);
                XmlElement root = xDoc.CreateElement("INPUT");
                XmlElement xe = xDoc.CreateElement("NSRSBH");
                xe.InnerText = NSRSBH;
                root.AppendChild(xe);
                root.AppendChild(addNode("FJH", FJH));
                root.AppendChild(addNode("KPZDH", KPZDH));
                root.AppendChild(addNode("FPLSH", FPLSH));
                root.AppendChild(addNode("XPLSH", XPLSH));
                root.AppendChild(addNode("FPZL", FPZL));
                root.AppendChild(addNode("KPLX", KPLX));
                root.AppendChild(addNode("XFSH", XFSH));
                root.AppendChild(addNode("XFMC", XFMC));
                root.AppendChild(addNode("XFDZDH", XFDZDH));
                root.AppendChild(addNode("XFYHZH", XFYHZH));
                root.AppendChild(addNode("GFSH", GFSH));
                root.AppendChild(addNode("GFMC", GFMC));
                root.AppendChild(addNode("GFDZDH", GFDZDH));
                root.AppendChild(addNode("GFYHZH", GFYHZH));
                root.AppendChild(addNode("KPR", KPR));
                root.AppendChild(addNode("SKR", SKR));
                root.AppendChild(addNode("FHR", FHR));
                root.AppendChild(addNode("YFPDM", YFPDM));
                root.AppendChild(addNode("YFPHM", YFPHM));
                root.AppendChild(addNode("HPTZDBH", HPTZDBH));
                root.AppendChild(addNode("QDBZ", QDBZ));
                root.AppendChild(addNode("JSHJ", JSHJ));
                root.AppendChild(addNode("HJJE", HJJE));
                root.AppendChild(addNode("HJSE", HJSE));
                root.AppendChild(addNode("BMBBH", BMBBH));
                root.AppendChild(addNode("BZ", BZ));
                root.AppendChild(addNode("YYSBZ", YYSBZ));
                //推送方式
                PushCount = COMMON_NODE.Length;
                string tmp = InvoicePushMethodModel.ToXml(COMMON_NODE);
                xe = xDoc.CreateElement("COMMON_NODES");
                XmlAttribute xa = xDoc.CreateAttribute("szie");
                xa.Value = PushCount.ToString();
                xe.Attributes.Append(xa);
                xe.InnerXml = tmp;
                root.AppendChild(xe);
                //分录
                MXCOUNT = FPXMXX.Length;
                tmp = InvoiceIssueEntryModel.ToXML(FPXMXX);
                xe = xDoc.CreateElement("FPXMXX");
                xa = xDoc.CreateAttribute("COUNT");
                xa.Value = MXCOUNT.ToString();
                xe.Attributes.Append(xa);
                root.AppendChild(xe);
                return xDoc.InnerXml;
            }
            catch (Exception)
            {

                throw;
            }
        }
        private XmlElement addNode(string name, object val)
        {
            XmlDocument xDoc = new XmlDocument();
            XmlElement xe = xDoc.CreateElement(name);
            xe.InnerText = val.ToString();
            return xe;
        }

    }
    public class InvoiceIssueEntryModel
    {
        /// <summary>
        /// 发票行性质
        /// </summary>
        public string FPHXZ { get; set; }
        /// <summary>
        /// 项目名称
        /// </summary>
        public string XMMC { get; set; }
        /// <summary>
        /// 规格型号
        /// </summary>
        public string GGXH { get; set; }
        /// <summary>
        /// 计量单位
        /// </summary>
        public string JLDW { get; set; }
        /// <summary>
        /// 项目数量
        /// </summary>
        public decimal XMSL { get; set; }
        /// <summary>
        /// 不含税单价
        /// </summary>
        public decimal XMDJ { get; set; }
        /// <summary>
        /// 不含税金额
        /// </summary>
        public decimal XMJE { get; set; }
        /// <summary>
        /// 税率
        /// </summary>
        public decimal SL { get; set; }
        /// <summary>
        /// 税额
        /// </summary>
        public decimal SE { get; set; }
        /// <summary>
        /// 含税单价
        /// </summary>
        public decimal HSDJ { get; set; }
        /// <summary>
        /// 含税金额
        /// </summary>
        public decimal HSJE { get; set; }
        /// <summary>
        /// 商品编码
        /// </summary>
        public string SPBM { get; set; }
        /// <summary>
        /// 企业自编码
        /// </summary>
        public string QYZXBM { get; set; }
        /// <summary>
        /// 是否享受优惠政策
        /// </summary>
        public string SFYHZC { get; set; }
        /// <summary>
        /// 优惠政策内容
        /// </summary>
        public string YHZCNR { get; set; }
        /// <summary>
        /// 零税率标识
        /// </summary>
        public string LSLBS { get; set; }
        public static string ToXML(InvoiceIssueEntryModel[] im)
        {
            try
            {
                if (null == im || im.Length < 1)
                    return null;
                string resualt = "";
                for (int i = 0; i < im.Length; i++)
                {
                    var item = im[i];
                    resualt += string.Format("<FPXM>{0}<FPHXZ>{1}</FPHXZ>{0}<XMMC>{2}</XMMC>{0}<GGXH>{3}</GGXH>{0}<JLDW>{4}</JLDW>{0}<XMSL>{5}</XMSL>{0}<XMDJ>{6}</XMDJ>{0}<XMJE>{7}</XMJE>{0}<SL>{8}</SL>{0}<SE>{9}</SE>{0}<HSDJ>{10}</HSDJ>{0}<HSJE>{11}</HSJE>{0}<SPBM>{12}</SPBM>{0}<QYZXBM>{13}</QYZXBM>{0}<SFYHZC>{14}</SFYHZC>{0}<YHZCNR>{15}</YHZCNR>{0}<LSLBS>{0}", Environment.NewLine, item.FPHXZ, item.XMMC, item.GGXH, item.JLDW, item.XMSL, item.XMDJ, item.XMJE, item.SL, item.SE, item.HSDJ, item.HSJE, item.SPBM, item.QYZXBM, item.SFYHZC, item.YHZCNR, item.LSLBS);
                }
                return resualt;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static InvoiceIssueEntryModel[] ResolveXML(string xmlData)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlData))
                    return null;
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlData);
                XmlElement xl = xDoc.DocumentElement;
                XmlNodeList xList = xl.GetElementsByTagName("FPXMXX");
                if (null == xList || xList.Count < 1)
                    return null;
                xl = (XmlElement)xList[0];
                int xCount = Convert.ToInt32(xl.Attributes["COUNT"].Value);
                if (xCount < 1)
                    return null;
                InvoiceIssueEntryModel[] im = new InvoiceIssueEntryModel[xCount];
                xList = xl.GetElementsByTagName("FPXM");
                for (int i = 0; i < xCount; i++)
                {
                    XmlNode xn = xList[i];
                    im[i] = new InvoiceIssueEntryModel
                    {
                        FPHXZ = xn.SelectSingleNode("FPHXZ").InnerText,
                        GGXH = xn.SelectSingleNode("GGXH").InnerText,
                        HSDJ = Convert.ToDecimal(xn.SelectSingleNode("HSDJ").InnerText),
                        HSJE = Convert.ToDecimal(xn.SelectSingleNode("HSJE").InnerText),
                        JLDW = xn.SelectSingleNode("JLDW").InnerText,
                        LSLBS = xn.SelectSingleNode("LSLBS").InnerText,
                        QYZXBM = xn.SelectSingleNode("QYZXBM").InnerText,
                        SE = Convert.ToDecimal(xn.SelectSingleNode("SE").InnerText),
                        SFYHZC = xn.SelectSingleNode("SFYHZC").InnerText,
                        SL = Convert.ToDecimal(xn.SelectSingleNode("SL").InnerText),
                        SPBM = xn.SelectSingleNode("SPBM").InnerText,
                        XMDJ = Convert.ToDecimal(xn.SelectSingleNode("XMDJ").InnerText),
                        XMJE = Convert.ToDecimal(xn.SelectSingleNode("XMJE").InnerText),
                        XMMC = xn.SelectSingleNode("XMMC").InnerText,
                        XMSL = Convert.ToDecimal(xn.SelectSingleNode("XMSL").InnerText),
                        YHZCNR = xn.SelectSingleNode("YHZCNR").InnerText
                    };
                }
                return im;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
    /// <summary>
    /// 推送方式模型
    /// </summary>
    public class InvoicePushMethodModel
    {
        /// <summary>
        /// 推送方式名 SJ,EMAIL
        /// </summary>
        public string NAME { get; set; }
        /// <summary>
        /// 推送方式值手机20字符，邮箱50
        /// </summary>
        public string VALUE { get; set; }
        public static InvoicePushMethodModel[] ResolveXML(string xmlString)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlString))
                    return null;
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlString);
                XmlElement xe = xDoc.DocumentElement;
                XmlNodeList xList = xe.GetElementsByTagName("COMMON_NODES");
                if (null == xList || xList.Count < 1)
                    return null;
                xe = (XmlElement)xList[0];
                int nCount = Convert.ToInt32(xe.Attributes["size"].Value);
                if (nCount < 1)
                    return null;
                xList = xe.GetElementsByTagName("COMMON_NODE");
                InvoicePushMethodModel[] im = new InvoicePushMethodModel[nCount];
                for (int i = 0; i < nCount; i++)
                {
                    XmlNode xn = xList[i];
                    im[i] = new InvoicePushMethodModel
                    {
                        NAME = xn.SelectSingleNode("NAME").InnerText,
                        VALUE = xn.SelectSingleNode("VALUE").InnerText
                    };
                }
                return im;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public static string ToXml(InvoicePushMethodModel[] invPushMethod)
        {
            try
            {
                if (null == invPushMethod || invPushMethod.Length < 1)
                    return null;
                string resualt = "";
                for (int i = 0; i < invPushMethod.Length; i++)
                {
                    resualt += string.Format("<COMMON_NODE>{0}<NAME>{1}</NAME>{0}<VALUE>{2}</VALUE>{0}</COMMON_NODE>{0}", Environment.NewLine, invPushMethod[i].NAME, invPushMethod[i].VALUE);
                }
                return resualt;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
    public class InvoiceReturnModel
    {
        public int CODE { get; set; }
        public string MESS { get; set; }
        public string FPLSH { get; set; }
        public string XPLSH { get; set; }
        public string FPDM { get; set; }
        public string FPHM { get; set; }
        public string KPSJ { get; set; }
        public string FPMW { get; set; }
        public string FWM { get; set; }
        public string EWM { get; set; }
        public string EWM51 { get; set; }
        public string JSHJ { get; set; }
        public string HJJE { get; set; }
        public string HJSE { get; set; }
        public string SIGN { get; set; }
        public string SYH { get; set; }
        public string SXH { get; set; }
        public string QDBZ { get; set; }
        public string BSQS { get; set; }
        public string BZ { get; set; }
        public void ResolveXML(string xmlData)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlData))
                    throw new Exception("数据不能为空！");
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlData);
                XmlElement xl = xDoc.DocumentElement;
                if (null == xl)
                    throw new Exception("数据格式不正确！");
                var xList = xl.GetElementsByTagName("RETURN");
                if (xList.Count < 1)
                    throw new Exception("数据格式不正确！");
                var xl1 = (XmlElement)xList[0];
                xList = xl1.GetElementsByTagName("CODE");
                if ("00000" != xList[0].InnerText)
                    throw new Exception("请求失败:" + MESS);
                CODE = xList.Count < 1 ? -1 : Convert.ToInt32(xList[0].InnerText);
                xList = xl1.GetElementsByTagName("MESS");
                MESS = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl.GetElementsByTagName("DATA");
                if (xList.Count < 1)
                    throw new Exception("数据格式不正确！");
                xl1 = (XmlElement)xList[0];
                xList = xl1.GetElementsByTagName("FPLSH");
                FPLSH = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("XPLSH");
                XPLSH = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("FPDM");
                FPDM = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("FPHM");
                FPHM = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("KPSJ");
                KPSJ = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("FPMW");
                FPMW = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("FWM");
                FWM = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("EWM");
                EWM = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("EWM51");
                EWM51 = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("JSHJ");
                JSHJ = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("HJJE");
                HJJE = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("HJSE");
                HJSE = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("SIGN");
                SIGN = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("SYH");
                SYH = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("SXH");
                SXH = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("QDBZ");
                QDBZ = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("BSQS");
                BSQS = xList.Count < 1 ? "" : xList[0].InnerText;
                xList = xl1.GetElementsByTagName("BZ");
                BZ = xList.Count < 1 ? "" : xList[0].InnerText;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
    #endregion
    #region 金税盘发票库存查询协议
    public class TaxCardInvoiceStockQueryModel
    {
        public string NSRSBH { get; set; }
        public string FJH { get; set; }
        public string FPZL { get; set; }
        public override string ToString()
        {
            try
            {
                string xml = string.Format("<?xml version=\"1.0\" encoding=\"GBK\"?>{0}<INPUT>{0}<NSRSBH>{1}</NSRSBH>{0}<FJH>{2}</FJH>{0}<FPZL>{3}</FPZL>{0}</INPUT>", Environment.NewLine, NSRSBH, FJH, FPZL);
                return xml;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
    public class TaxCardInvoiceStockQueryReturnModel
    {
        public string CODE { get; set; }
        public string MESS { get; set; }
        public TaxCardInvoiceStockEntryModel[] FPJXX { get; set; }
        public int FPJCOUNT { get; set; }

        public void ResolveXml(string xmlData)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlData))
                    throw new Exception("数据不能为空！");
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlData);
                XmlElement xe = xDoc.DocumentElement;
                XmlNodeList xList = xe.GetElementsByTagName("RETURN");
                if (null == xList || xList.Count < 1)
                    return;
                XmlNode xn = xList[0];
                CODE = xn.SelectSingleNode("CODE").InnerText;
                MESS = xn.SelectSingleNode("MESS").InnerText;
                if (CODE != "000000")
                    throw new Exception("请求失败:" + MESS);
                FPJXX = TaxCardInvoiceStockEntryModel.ResolveXml(xmlData);
                if (null == FPJXX || FPJXX.Length < 1)
                    return;
                FPJCOUNT = FPJXX.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
    public class TaxCardInvoiceStockEntryModel
    {
        /// <summary>
        /// 发票卷顺序
        /// </summary>
        public int FPJSXH { get; set; }
        /// <summary>
        /// 发票种类
        /// </summary>
        public string FPZL { get; set; }
        /// <summary>
        /// 发票代码
        /// </summary>
        public string FPDM { get; set; }
        /// <summary>
        /// 起始号码
        /// </summary>
        public string QSHM { get; set; }
        /// <summary>
        /// 发票份数
        /// </summary>
        public int FPFS { get; set; }
        /// <summary>
        /// 剩余份数
        /// </summary>
        public int SYFS { get; set; }
        /// <summary>
        /// 购买日期
        /// </summary>
        public DateTime GMRQ { get; set; }


        public static TaxCardInvoiceStockEntryModel[] ResolveXml(string xmlData)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlData))
                    return null;
                XmlDocument xDoc = new XmlDocument();
                xDoc.LoadXml(xmlData);
                XmlElement xe = xDoc.DocumentElement;
                XmlNodeList xList = xe.GetElementsByTagName("DATA");
                if (null == xList || xList.Count < 1)
                    return null;
                xe = (XmlElement)xList[0];
                XmlNode xn = xe.SelectSingleNode("FPJXX");
                int sCount = Convert.ToInt32(xn.Attributes["COUNT"].Value);
                if (sCount < 1)
                    return null;
                xe = (XmlElement)xn;
                xList = xe.GetElementsByTagName("FPJ");
                if (null == xList || xList.Count != sCount)
                    return null;
                TaxCardInvoiceStockEntryModel[] te = new TaxCardInvoiceStockEntryModel[sCount];
                for (int i = 0; i < sCount; i++)
                {
                    xn = xList[i];
                    te[i] = new TaxCardInvoiceStockEntryModel
                    {
                        FPDM = xn.SelectSingleNode("FPDM").InnerText,
                        FPFS = Convert.ToInt32(xn.SelectSingleNode("FPFS").InnerText),
                        FPJSXH = Convert.ToInt32(xn.SelectSingleNode("FPJSXH").InnerText),
                        FPZL = xn.SelectSingleNode("FPZL").InnerText,
                        GMRQ = DateTime.ParseExact(xn.SelectSingleNode("GMRQ").InnerText, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture),
                        QSHM = xn.SelectSingleNode("QSHM").InnerText,
                        SYFS = Convert.ToInt32(xn.SelectSingleNode("SYFS").InnerText)
                    };
                }
                return te;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
    #endregion

}

